package com.momo.service.async;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import com.momo.common.common.JSONResult;
import com.momo.common.error.RedisKeyEnum;
import com.momo.common.req.sysSetting.authority.RoleReq;
import com.momo.common.util.RedisUtil;
import com.momo.mapper.dataobject.*;
import com.momo.mapper.mapper.sysSetting.AuthorityMapper;
import com.momo.mapper.mapper.sysmain.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by MOMO on 2019/3/22.
 * 异步  初始化权限到redis
 */
@Service
@Slf4j
public class InitAclToRedisAsync {
    @Autowired
    private AuthorityMapper authorityMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private AclAsync aclAsync;
    @Autowired
    private RedisUtil redisUtil;

    public JSONResult initAclToRedis() {
        //用户列表
        List<UserDO> allUser = sysUserMapper.getAllUser();
        allUser.forEach(userDO -> {
            //根据用户id 得到 角色 id 列表
            List<Long> roles = authorityMapper.rolesByUserId(userDO.getId());
            //用户id  角色ids
            aclAsync.userRolesToRedis(userDO.getId(), roles);
            roles.forEach(aLong -> {
                //根据角色id 得到 权限列表
                List<RoleAclDO> aclIds = authorityMapper.allAclsByRoleId(aLong);
                //  角色id 权限点 ids
                aclAsync.roleAclsToRedis(aLong, aclIds);
            });
        });
        //所有权限模块
        List<AclModuleDO> aclModuleDOS = authorityMapper.getAllAclModule(null);
        Multimap<Long, AclModuleDO> multimapModule = ArrayListMultimap.create();
        Set<Long> setAclModule = Sets.newHashSet();
        aclModuleDOS.forEach(aclModuleDO -> {
            multimapModule.put(aclModuleDO.getSysAclPermissionType(), aclModuleDO);
            setAclModule.add(aclModuleDO.getSysAclPermissionType());
        });
        setAclModule.forEach(aLong -> {
            List<AclModuleDO> list = (List<AclModuleDO>) multimapModule.get(aLong);
            if (!CollectionUtils.isEmpty(list)) {
                aclAsync.batchAclModuleToRedis(list, aLong);
            }
        });
        //所有权限点
        List<AclDO> aclDOS = authorityMapper.getAllAcl(null, null);
        Multimap<Long, AclDO> multimapAcl = ArrayListMultimap.create();
        Set<Long> setAcl = Sets.newHashSet();
        aclDOS.forEach(aclDO -> {
            multimapAcl.put(aclDO.getSysAclPermissionType(), aclDO);
            setAcl.add(aclDO.getSysAclPermissionType());
        });
        setAcl.forEach(aLong -> {
            List<AclDO> list = (List<AclDO>) multimapAcl.get(aLong);
            if (!CollectionUtils.isEmpty(list)) {
                aclAsync.batchAclToRedis(list, aLong);
            }
        });
        //所有角色
        List<RoleDO> roleDOS = authorityMapper.getAllRoles();
        Map<String, String> adminRole = Maps.newHashMap();
        Map<String, String> commonRole = Maps.newHashMap();
        roleDOS.forEach(roleDO -> {
            RoleReq.RoleReqRedis roleReqRedis = RoleReq.roleReqRedis(roleDO);
            if ("0".equals(roleReqRedis.getSysRoleType())) {
                adminRole.put(String.valueOf(RedisKeyEnum.REDIS_ADMIN_ROLE_STR.getKey() + roleReqRedis.getGroupId()), JSONObject.toJSONString(roleReqRedis));
            }
            commonRole.put(String.valueOf(RedisKeyEnum.REDIS_ROLE_STR.getKey() + roleReqRedis.getId()), JSONObject.toJSONString(roleReqRedis));
        });
        aclAsync.adminRole(adminRole);
        aclAsync.commonRole(commonRole);

        return JSONResult.ok("已为您异步初始化权限到redis");
    }

}
